import Vue from 'vue'
import VueRouter from 'vue-router'	//默认就是index.js
import login from '../views/login'   //默认后缀就是index.vue
import layout from '../views/layout'
import notfound from '../views/notfound'
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const routes = [
	// 跳转到首页
	{
		path:"/",
		redirect:"/layout/home",
		meta:{
			isMenu:false
		}
	},
	//跳转到登录
	{
	  path: '/login',
	  name: '登录',
	  meta:{
	  	isMenu:false
	  },
	  component: login
	},
	//框架页的配置
	{
	  path: '/layout',
	  name: '框架',
	  meta:{
	  	isMenu:false
	  },
	  component: layout,
	  children:[
		  {
			  path:"home",
			  component: () => import(/* webpackChunkName: "about" */ '@/views/home')
		  }
	  ]
	},
	//商品路由配置
	{
	  path: '/pms',
	  name: '商品',
	  meta:{
	  	isMenu:true,
		"title":"商品",
		"icon":"iconfont icon-shangpin-tianchong"
	  },
	  component: layout,
	  redirect:"/pms/product",
	  children:[
		  // 懒加载，用的加载，不用的时候不加载
		  {
		  path:"product",
		  meta:{
		  		"title":"商品列表" ,
				"icon":"iconfont icon-liebiao1"
		  },
		  // @表示src的路径
		  component: () => import(/* webpackChunkName: "about" */ '@/views/pms/product')
		  },
		  {
		  path:"productadd",
		  meta:{
		  		"title":"添加商品",
				"icon":"iconfont icon-tianjia"
		  },
		  // @表示src的路径
		  component: () => import(/* webpackChunkName: "about" */ '@/views/pms/productadd')
		  },
		  {
		  path:"category",
		  meta:{
		  		"title":"商品分类",
				 "icon":"iconfont icon-haofangtuo400iconfontzhuzhaileixing"
		  },
		  // @表示src的路径
		  component: () => import(/* webpackChunkName: "about" */ '@/views/pms/category')
		  },
		  {
		  path:"producttype",
		  meta:{
		  		"title":"商品类型",
				"icon":"iconfont icon-haofangtuo400iconfontzhuzhaileixing"
		  },
		  // @表示src的路径
		  component: () => import(/* webpackChunkName: "about" */ '@/views/pms/producttype')
		  },
		  {
		  path:"brand",
		  meta:{
		  		"title":"品牌管理",
				 "icon":"iconfont icon-pinpaitemai"
		  },
		  // @表示src的路径
		  component: () => import(/* webpackChunkName: "about" */ '@/views/pms/brand')
		  }
	  ]
	},
	// 404路由；配置
	{
	  path: '/*',
	  meta:{
	  	isMenu:false
	  },
	  name: '未找到',
	  component: notfound
	}
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
	//检测是否登录，判断cookie中是否有authoration，如果有凭证，就说明已登录，否则就是未登录。
	let authorization = Cookies.get("Authorization");
	if("/login" == to.path){
		next();
		return;
	}
	if(authorization){
		next()
		return;
	}
	next("/login");
	return;
})

export default router
